from PyQt5.QtCore import *
from examples.example_calculator.calc_conf import *
from examples.example_calculator.calc_node_base import *
from nodeeditor.utils import dumpException

#设置某任务的执行频率
class CalcGraphicsNode(QDMGraphicsNode):#节点外壳
    def initSizes(self):
        super().initSizes()
        self.width = 180
        self.height = 80
        self.edge_roundness = 6
        self.edge_padding = 0
        self.title_horizontal_padding = 8
        self.title_vertical_padding = 10

    def initAssets(self):
        super().initAssets()
        self.icons = QImage("icons/status_icons.png")

    def paint(self, painter, QStyleOptionGraphicsItem, widget=None):
        super().paint(painter, QStyleOptionGraphicsItem, widget)

        offset = 24.0
        if self.node.isDirty(): offset = 0.0
        if self.node.isInvalid(): offset = 48.0

        painter.drawImage(
            QRectF(-10, -10, 24.0, 24.0),
            self.icons,
            QRectF(offset, 0, 24.0, 24.0)
        )
        
class CalcTimeContent(QDMNodeContentWidget):
    def initUI(self):
        self.edit1 = QLineEdit("0.1", self)
        self.edit1.setAlignment(Qt.AlignLeft)
        self.edit1.setObjectName(self.node.content_label_objname)
        
        self.layout2 = QHBoxLayout()#横框
        self.layout2.setContentsMargins(10,0,10,0)
        self.setLayout(self.layout2)
        
        self.edit3 = QLineEdit("#请输入任务编号和频率秒数", self)
        self.edit3.setAlignment(Qt.AlignLeft)
        self.edit3.setObjectName(self.node.content_label_objname)
        self.edit3.setEnabled(False)#禁用文本框
        
        self.edit3.setMinimumWidth(90)
        self.edit3.setMinimumHeight(30)
        self.edit1.setMinimumHeight(30)
        self.edit1.setMaximumWidth(40)
        
        self.layout2.addWidget(self.edit1)
        self.layout2.addWidget(self.edit3)
        
    
    def serialize(self):
        res = super().serialize()
        res['value1'] = self.edit1.text()
        res['value2'] = self.edit3.text()
        return res

    def deserialize(self, data, hashmap={}):
        res = super().deserialize(data, hashmap)
        try:
            value1 = data['value1']
            self.edit1.setText(value1)
            value2 = data['value2']
            self.edit3.setText(value2)
            return True & res
        except Exception as e:
            dumpException(e)
        return res


@register_node(OP_NODE_Time)
class CalcNode_Time(CalcNode):
    icon = "icons/add.png"
    op_code = OP_NODE_Time
    op_title = "延迟"
    content_label = ""
    content_label_objname = "calc_node_time"
    
    def __init__(self, scene):
        super().__init__(scene, inputs=[], outputs=[1])
        self.eval()
    
    def initInnerClasses(self):
        self.content = CalcTimeContent(self)
        self.grNode = CalcGraphicsNode(self)
        self.content.edit1.textChanged.connect(self.onInputChanged)
        self.content.edit3.textChanged.connect(self.onInputChanged)
    
    def evalImplementation(self):
        self.value = self.setMyText()
        
        self.markDirty(False)
        self.markInvalid(False)

        self.markDescendantsInvalid(False)
        self.markDescendantsDirty()

        self.grNode.setToolTip("")

        self.evalChildren()

        return self.value
    
    def setMyText(self):
        #设置输入的文本，让其拥有只执行一次的功能
        str02 = self.content.edit1.text()#频率秒数
        
        str03 = 'time.sleep(' + str02 + ')'
        self.content.edit3.setText(str03)
        return str03

